package btanalyzer.db.dao;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import btanalyzer.db.model.Metadata;

@Component
public class MetadataDaoImpl extends GenericDaoAbstract<Metadata, Long> implements MetadataDao {

	@Override
	public long countByDwonloadTaskAndSizeInBytes(long minSizeInBytes,
			long maxSizeInBytes, long diggTaskId) {
		TypedQuery<Long> q = getEntityManager().createQuery("select count(m) from Metadata m " +
				"where m.sizeInBytes >= :minSizeInBytes and m.sizeInBytes <= " +
				":maxSizeInBytes and m.torrnet.diggedSite.diggTask.id = " +
				":diggTaskId", Long.class);
		q.setParameter("minSizeInBytes", minSizeInBytes);
		q.setParameter("maxSizeInBytes", maxSizeInBytes);
		q.setParameter("diggTaskId", diggTaskId);
		return q.getSingleResult();
	}

	@Override
	public long countBySizeInBytes(long minSizeInBytes, long maxSizeInBytes) {
		TypedQuery<Long> q = getEntityManager().createQuery("select count(m) from Metadata m " +
				"where m.sizeInBytes >= :minSizeInBytes and m.sizeInBytes <= " +
				":maxSizeInBytes", Long.class);
		q.setParameter("minSizeInBytes", minSizeInBytes);
		q.setParameter("maxSizeInBytes", maxSizeInBytes);
		return q.getSingleResult();
	}

}
